const express = require('express')
const router = express.Router()
const mysql = require('../util/mysql')

// 初始化文章列表
router.get('/', async (req, res, next) => {
  // const token = req.query.token
  const promisePool = mysql.sql()
  const info = await promisePool.query('select * from user join art on art.uid=user.uid order by art.date desc')
  res.send({ msg: info[0] })
})

// 展示文章
router.get('/info', async (req, res, next) => {
  const uid = req.query.uid
  const aid = req.query.aid
  const promisePool = mysql.sql()
  const info = await promisePool.query('select header,content,name,pho,art.date from user join art on art.uid=user.uid where aid=? and art.uid =?', [aid, uid])
  res.send({ code: 1, msg: info[0] })
})

// 浏览数量的增加
router.get('/look', async (req, res, next) => {
  const uid = req.query.uid
  const aid = req.query.aid
  const promisePool = mysql.sql()
  const info = await promisePool.query('select look from art where aid=? and uid =?', [aid, uid])
  const look = info[0][0].look + 1
  await promisePool.query('update art set look=? where aid=? and uid=?', [look, aid, uid])
  res.send('ok')
})

// 点赞
// router.get('/zan', async (req, res, next) => {
//   const uid = req.query.uid
//   const aid = req.query.aid
//   const promisePool = mysql.sql()
//   const info = await promisePool.query('select zan from art where aid=? and uid =?', [aid, uid])
//   const zan = info[0][0].zan + 1
//   await promisePool.query('update art set zan=? where aid=? and uid=?', [zan, aid, uid])
//   res.send('ok')
// })
// 取消点赞
// router.put('/zan', async (req, res, next) => {
//   // const uid = req.query.uid
//   // const aid = req.query.aid
//   // const promisePool = mysql.sql()
//   // const info = await promisePool.query('select zan from art where aid=? and uid =?', [aid, uid])
//   // console.log(info[0][0].zan)
//   // await promisePool.query('update art set zan=? where aid=? and uid=?', [zan, aid, uid])
//   res.send('ok')
// })

// 渲染用户列表
router.get('/user', async (req, res, next) => {
  const token = req.query.token
  const promisePool = mysql.sql()
  const info = await promisePool.query('select pho,name from user where token != ?', [token])
  res.send({ code: 1, info: info[0] })
})

module.exports = router
